<?php

/**
 * Class SubscribeController
 */
class SubscribeController extends MyController
{

    /**
     * Subscription managerment
     */
    public function actionIndex()
    {
        $model             = new Subscription('search');
        if (isset($_GET['Subscription']))
            $model->attributes = $_GET['Subscription'];
        $this->render('admin', array(
            'model' => $model,
        ));
        //$this->render('index');
    }

    public function actionUnsubscribe()
    {
        if (isset($_GET['email']) && isset($_GET['token']))
        {
            $email = $_GET['email'];
            $token = $_GET['token'];

            $tokenMine = MyEmail::getToken($email);
            if ($token === $tokenMine)
            {
                $unsubscribe = Unsubscribe::model()->find('email=:email', array(
                    'email' => $email,
                ));

                if ($unsubscribe == null)
                {
                    $unsubscribe        = new Unsubscribe();
                    $unsubscribe->email = $email;
                }

                $unsubscribe->created_at = time();
                if ($unsubscribe->save())
                {
                    echo 'You have been unsubcrible successful. Thank you!';
                }
                else
                {
                    echo 'Sorry, system error please try again later!';
                }
            }
        }
    }

}

/** 
 * End file SubscribeController
 */
